#!/bin/bash

# 修复在分页方法中错误使用 selectList 的问题

echo "开始修复分页方法中的 selectList 问题..."
echo "=========================================="

# 定义需要修复的文件列表
files=(
    "fuint-application/src/main/java/com/fuint/common/service/impl/CouponGroupServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/ArticleServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/StockServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/CateServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/UserActionServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/SendSmsServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/BookCateServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/MemberGroupServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/BannerServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/DutyServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/StoreServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/UserGradeServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/SettlementServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/ConfirmLogServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/SmsTemplateServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/BookServiceImpl.java"
)

# 备份文件
echo "创建备份..."
for file in "${files[@]}"; do
    if [ -f "$file" ]; then
        cp "$file" "$file.backup.$(date +%Y%m%d_%H%M%S)"
        echo "已备份: $file"
    fi
done

echo ""
echo "开始修复文件..."

# 修复每个文件
for file in "${files[@]}"; do
    if [ -f "$file" ]; then
        echo "正在修复: $file"
        
        # 创建临时文件
        temp_file=$(mktemp)
        
        # 使用 awk 进行复杂的替换
        awk '
        BEGIN { 
            in_pagination_method = 0
            pagination_method_lines = 0
        }
        
        # 检测分页方法开始
        /PaginationRequest.*paginationRequest/ {
            in_pagination_method = 1
            pagination_method_lines = 0
            print $0
            next
        }
        
        # 在分页方法中
        in_pagination_method == 1 {
            pagination_method_lines++
            
            # 如果遇到方法结束或新方法开始，退出分页方法
            if (/^[[:space:]]*}[[:space:]]*$/ && pagination_method_lines > 20) {
                in_pagination_method = 0
                print $0
                next
            }
            if (/^[[:space:]]*public/ || /^[[:space:]]*private/ || /^[[:space:]]*protected/) {
                in_pagination_method = 0
                print $0
                next
            }
            
            # 替换 selectList 为 selectPage 相关逻辑
            if (/List<.*> dataList = .*\.selectList\(lambdaQueryWrapper\);/) {
                # 提取实体类型
                match($0, /List<([^>]+)>/, entity_type)
                entity = entity_type[1]
                
                # 提取 mapper 名称
                match($0, /([a-zA-Z0-9_]+)\.selectList/, mapper_name)
                mapper = mapper_name[1]
                
                # 生成替换代码
                indent = ""
                match($0, /^([[:space:]]*)/, indent_match)
                if (length(indent_match) > 0) {
                    indent = indent_match[1]
                }
                
                print indent "Page<" entity "> page = new Page<>(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());"
                print indent "Page<" entity "> pageResult = " mapper ".selectPage(page, lambdaQueryWrapper);"
                print indent "List<" entity "> dataList = pageResult.getRecords();"
                next
            }
            
            # 跳过错误的 PageRequest 和 PageImpl 创建
            if (/PageRequest pageRequest = PageRequest\.of/) {
                next
            }
            if (/PageImpl pageImpl = new PageImpl\(dataList, pageRequest, pageResult\.getTotal\(\)\);/) {
                next
            }
            
            # 修复 PaginationResponse 创建中的错误引用
            if (/PaginationResponse.*paginationResponse = new PaginationResponse\(pageImpl,/) {
                gsub(/pageImpl/, "pageResult", $0)
                print $0
                next
            }
            
            # 修复 setTotalPages 和 setTotalElements 调用
            if (/paginationResponse\.setTotalPages\(pageResult\.getPages\(\)\);/) {
                gsub(/pageResult\.getPages\(\)/, "pageResult.getPages()", $0)
                print $0
                next
            }
            if (/paginationResponse\.setTotalElements\(pageResult\.getTotal\(\)\);/) {
                gsub(/pageResult\.getTotal\(\)/, "pageResult.getTotal()", $0)
                print $0
                next
            }
        }
        
        # 默认输出
        { print $0 }
        ' "$file" > "$temp_file"
        
        # 替换原文件
        mv "$temp_file" "$file"
        echo "  ✅ 修复完成"
    else
        echo "  ❌ 文件不存在: $file"
    fi
done

echo ""
echo "修复完成！"
echo "=========================================="
echo "请运行以下命令验证修复结果："
echo "bash check_selectlist_in_pagination.sh"
